package com.fy56.platform.logistics.restful.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fy56.platform.logistics.utils.HttpClientUtils;
import com.fy56.platform.logistics.utils.HttpUtil;
import com.fy56.platform.logistics.utils.test;
import com.google.common.collect.Maps;
import com.zeus.base.privilege.utils.HttpClientUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

/**
 * Created by Administrator on 2016-03-03.
 */
public class Test {
    public static InputStream sendPostRequestByJava(String reqURL, String sendData){
        HttpURLConnection httpURLConnection = null;
        OutputStream out = null; //写
        InputStream in = null;   //读
        int httpStatusCode = 0;  //远程主机响应的HTTP状态码
        try{
            URL sendUrl = new URL(reqURL);
            httpURLConnection = (HttpURLConnection)sendUrl.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);        //指示应用程序要将数据写入URL连接,其值默认为false
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setConnectTimeout(30000); //30秒连接超时
            httpURLConnection.setReadTimeout(30000);    //30秒读取超时

            out = httpURLConnection.getOutputStream();
            out.write(sendData.toString().getBytes());

            //清空缓冲区,发送数据
            out.flush();

            //获取HTTP状态码
            httpStatusCode = httpURLConnection.getResponseCode();

            in = httpURLConnection.getInputStream();
            byte[] byteDatas = new byte[in.available()];
            in.read(byteDatas);
        }catch(Exception e){
        }finally{
            if(out != null){
                try{
                    out.close();
                }catch (Exception e){
                }
            }
//            if(in != null){
//                try{
//                    in.close();
//                }catch(Exception e){
//                }
//            }
            if(httpURLConnection != null){
                httpURLConnection.disconnect();
                httpURLConnection = null;
            }
        }
        return in;
    }
    public static void wxPost(String uri, JSONObject paramJson, String fileName) {
        try {
            URL url = new URL(uri);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    .openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(
                    httpURLConnection.getOutputStream());
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            // 开始获取数据
            BufferedInputStream bis = new BufferedInputStream(
                    httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("D:/upFiles/"
                    + fileName + ".png"));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
            bis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {




        // 获得access_token

        String accessToken = getAccessToken();



        // 获得小程序二维码

        // POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

        String URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken;

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("path","pages/order/order?shareId=M100");
        jsonObject.put("width",430);



        try {
            wxPost(URL,jsonObject,"开单");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static BufferedImage createNewPic(String title,BufferedImage logo){



        BufferedImage image = new BufferedImage(500, 550, BufferedImage.TYPE_INT_RGB);



        //设置图片的背景色

        Graphics2D main = image.createGraphics();

        main.setColor(Color.white);

        main.fillRect(0, 0, 500, 550);



        //***********************插入中间广告图

        Graphics mainPic = image.getGraphics();



        if(logo!=null){

            mainPic.drawImage(logo, 40, 40, 400, 400, null);

            mainPic.dispose();

        }



        //***********************页面头部

        Graphics titleG = image.createGraphics();

        //设置区域颜色

        //titleG.setColor(Color.white);

        //填充区域并确定区域大小位置

        //titleG.fillRect(450, 50, 450, 50);

        //设置字体颜色，先设置颜色，再填充内容

        titleG.setColor(Color.BLACK);

        //设置字体

        Font titleFont = new Font("宋体", Font.BOLD, 14);

        titleG.setFont(titleFont);

        titleG.drawString(title, 200, 500);





        return image;

    }



    private static String getAccessToken(){

        String result = null;


        String appId="wx881c5f23c98d2b34";
        String secret="0c0c3c26dc46a4c53ef45723f6a2f1d0";

        try {

             result = HttpClientUtils.get("https://api.weixin.qq.com/cgi-bin/token" +

                     "?grant_type=client_credential&appid="+appId+"&secret="+secret, null);
        } catch (Exception e) {

            e.printStackTrace();

        }



        JSONObject jsonObject = JSON.parseObject(result);

        result = jsonObject.getString("access_token");

        System.out.println(result);

        return  result;

    }
}
